<template>
  <div class="user-index">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="80px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="4">
                  <el-form-item label="手机号" prop="id">
                    <el-input  placeholder="请输入手机号" size="mini" v-model.trim="id" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="用户名" prop="username">
                    <el-input  placeholder="请输入用户名" size="mini" v-model.trim="username" clearable></el-input>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="4">
                  <el-form-item label="昵称" prop="nickname">
                    <el-input  placeholder="请输入昵称" size="mini" v-model.trim="nickname" clearable></el-input>
                  </el-form-item>
                </el-col> -->
                <el-col :span="2">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
                <el-col :span="2">
                  <!-- <el-button size="mini" @click="handleExport" v-hasPermission="permission.user.postExportUser" class="search-btn" style="margin-left:-10px;">导出</el-button> -->
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>用户管理</span>
            <div class="btn-list">
              <el-button size="mini" @click="handleAdd" v-hasPermission="permission.add">新增</el-button>
              <!-- <el-button size="mini" @click="handleDeleteAll" v-hasPermission="permission.user.postUsersDelete" :disabled="isDelete">批量删除</el-button> -->
            </div>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @select-change="handleSelectChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="sex">
                <span>{{ scope.row.sex == 1 ? '男' : '女' }}</span>
              </template>
              <template slot-scope="scope" slot="create_time">
                <span>{{ scope.row.create_time | timeFix }}</span>
              </template>
              <template slot-scope="scope" slot="status">
                <el-switch v-model="scope.row.status" @change="changeStatus(scope.row)" :active-value="1" :inactive-value="0"></el-switch>
              </template>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" slot="actionMenu" label="操作" align="center" header-align="center" width="160">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleEdit(scope.row)" v-hasPermission="permission.edit" v-if="scope.row.username !== 'superAdmin'">修改</el-button>
                  <el-button type="text" size="small" @click="handleReset(scope.row)" v-hasPermission="permission.resetPassword" v-if="scope.row.username !== 'superAdmin'">重置密码</el-button>
                  <el-button type="text" size="small" @click="handleDelete(scope.row)" v-hasPermission="permission.delete" v-if="scope.row.id != userInfo.id && scope.row.username !== 'superAdmin'">删除</el-button>
                </template>
              </el-table-column>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 编辑弹窗 -->
    <index-edit
      @closeDialog="closeDialog('edit')"
      @confirmDialog="confirmDialog('edit')"
      @handleClose="closeDialog('edit')"
      v-if="dialogIndexEdit"
      :dialogVisible="dialogIndexEdit">
    </index-edit>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import cvueTable from '@/components/cvue-table'
import indexEdit from './indexEdit'
import {getClientHeight} from '@/util/tool'
export default {
  name: 'userIndex',
  components: {
    cvueTable,
    indexEdit
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'index', label: '', align: 'center', width: 58 },
          // { prop: 'id', label: '用户编号', align: 'left', sortable: true },
          { prop: 'username', label: '用户名', align: 'center', sortable: true },
          { prop: 'name', label: '昵称', align: 'center', sortable: true },
          { prop: 'role_name', label: '角色', align: 'center', sortable: true },
          { prop: 'mobile', label: '手机号', align: 'center', sortable: true },
          { prop: 'sex', label: '性别', align: 'center', sortable: true, slotStatus: true },
          { prop: 'create_time', label: '创建时间', align: 'center', sortable: true, slotStatus: true },
          { prop: 'status', label: '状态', align: 'center', sortable: true, slotStatus: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogIndexEdit: false,
      searchArr: [
        {label: 'ID', value: 'user_id'},
        {label: '账号', value: 'username'},
        {label: '用户名', value: 'nick_name'},
        {label: '手机号', value: 'phone'}
      ],
      actionType: 'add',
      rowData: {},
      selectIds: [],
      id: '',
      username: '',
      nickname: '',
      selectId: '',
      selectUsername: '',
      selectNickname: ''
    }
  },
  computed: {
    height () {
      var height = getClientHeight() - 170
      return height + 'px'
    },
    isDelete () {
      return this.selectIds.indexOf(this.userInfo.id) > -1
    },
    ...mapGetters(['token', 'userInfo'])
  },
  created () {
    this.handleList(this.selectId, this.selectUsername, this.selectNickname)
    // console.log(this.userInfo)
  },
  methods: {
    // 新增
    handleAdd () {
      this.actionType = 'add'
      this.dialogIndexEdit = true
    },
    // 编辑
    handleEdit (row) {
      this.actionType = 'edit'
      this.dialogIndexEdit = true
      this.rowData = row
    },
    // 密码重置
    handleReset (row) {
      this.$confirm('确定要重置密码吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        this.$store.dispatch('manage/ResetPassword', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    handleDelete (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        this.$store.dispatch('manage/UserDelete', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectId, this.selectUsername, this.selectNickname)
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    handleDeleteAll () {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.selectIds.length == 0) {
          this.$message({type: 'error', message: '至少选择一条'})
          return
        }
        var params = this.selectIds
        this.$store.dispatch('manage/DeleteUser', params).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.tablePage = 1
          this.handleList(this.selectId, this.selectUsername, this.selectNickname)
        }).catch((err) => {
          this.$message({type: 'error', message: err.msg})
        })
      }).catch(() => {
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.selectId, this.selectUsername, this.selectNickname)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.selectId, this.selectUsername, this.selectNickname)
    },
    // 表格选择
    handleSelectChange (val) {
      // console.log(val)
      var arr = []
      for (var i = 0; i < val.length; i++) {
        arr.push(val[i].id)
      }
      this.selectIds = arr
      // console.log(this.selectIds)
    },
    // 列表接口
    handleList (id, username, nickname) {
      this.tableLoading = true
      var params = {
        page: this.tablePage,
        limit: this.page.pageSize,
        mobile: id,
        username: username
        // nickname: nickname
      }
      this.$store.dispatch('manage/UserGet', params).then(res => {
        // console.log(res)
        var data = res.data.datas
        this.tableData = data
        // this.tableData.forEach((item, index) => {
        //   var rolesName = []
        //   item.roles.forEach((it, ix) => {
        //     rolesName.push(it.name)
        //   })
        //   item.rolesName = rolesName.join()
        // })
        this.page.total = res.data.count
        this.page.currentPage = this.tablePage
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogIndexEdit = false
        this.selectKey = ''
        this.selectValue = ''
        this.tablePage = 1
        this.handleList(this.selectId, this.selectUsername, this.selectNickname)
      }
    },
    handleSearch () {
      this.selectId = this.id
      this.selectUsername = this.username
      this.selectNickname = this.nickname
      this.tablePage = 1
      this.handleList(this.selectId, this.selectUsername, this.selectNickname)
    },
    handleExport () {
      this.tableLoading = true
      var params = {
        page: this.tablePage,
        limit: this.page.pageSize,
        id: this.selectId,
        username: this.selectUsername,
        nickname: this.selectNickname
      }
      this.$store.dispatch('manage/ExportUser', params).then(res => {
        var blob = res
        var objectUrl = URL.createObjectURL(blob)
        window.location.href = objectUrl
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 修改状态
    changeStatus (row) {
      // console.log(row)
      this.tableLoading = true
      var params = {
        id: row.id,
        enabled: row.enabled
      }
      this.$store.dispatch('manage/UpdateEnabled', params).then(res => {
        // console.log(res)
        this.$message({
          type: 'success',
          message: res.msg
        })
        this.tableLoading = false
      }).catch(err => {
        this.$message({type: 'error', message: err.msg})
        this.tableLoading = false
      })
    }
  }
}
</script>

<style lang="scss">
.user-index {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
